home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2372 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.2 KB  |  157 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: 20 Jan 1996 21:48:17 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4drnv1$cr@news.iag.net>
  8. References: <4dm889$3hs@neptunus.pi.net>
  9. NNTP-Posting-Host: pm1-orl30.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4dm889$3hs@neptunus.pi.net>, mv@pi.net says...
  13. >
  14. >Hello everybody,
  15. >
  16. >
  17. >Please take a look at this:
  18. <code snipped>
  19.  
  20. Below is a modified version of the posted code.  All comments are mine
  21. <hint about need for comments> and usually refer to modifications I have made.
  22.  
  23. The original problem appears to have been a side effect of the incorrect
  24. definition of the second parameter in get_switches. The c.l.c faq (Frequently
  25. Asked Question) list explains this in more detail.  It is available for 
  26. anonymous ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. /* #include <conio.h>   not ansi */
  32. #include <string.h>
  33. /* #include <alloc.h>   not ansi or needed (malloc is in stdlib.h) */
  34.  
  35.  
  36. #define MAXLEN    20
  37. #define MAXSWITCH 20
  38.  
  39. /* You should normally avoid using globals unless you have a specific */
  40. /* need for them.  They tend to make code hard to read.               */
  41. char *s = "dir /w:test/1/2/3/4";
  42. char *s2, *s3;
  43. char myswitches[MAXSWITCH][MAXLEN];
  44.  
  45.  
  46. /* int get_switches(char *cmd, char *switches[][MAXLEN])             */
  47. /* this is a pointer to a pointer to an array of MAXLEN chars. your  */
  48. /* compiler didn't generated numerous warnings as a result?      try */
  49. /* int get_switches(char *cmd, char switches[][MAXLEN])          or  */
  50. /* int get_switches(char *cmd, char *switches[MAXLEN])           or  */
  51. int get_switches(char *cmd, char switches[MAXSWITCH][MAXLEN])
  52.    {
  53.    int i = 0;
  54.    char *cpy;
  55.    char *saved;
  56.  
  57.    if (strlen(cmd) == 0)
  58.       {
  59.       return 0;
  60.       }
  61.    
  62.    /* Why malloc? You could just define saved as char saved[MAXLEN]; */
  63.    /* and set cpy = saved                                            */
  64.    cpy = (char *) malloc(MAXLEN);
  65.    if (cpy == NULL)
  66.       {
  67.       printf("Out of memory in %s on line: %d\n", __FILE__, __LINE__);
  68.       exit(EXIT_FAILURE);
  69.       }
  70.                   
  71.    /* strcpy(cpy, '\0'); Your compiler should have complained */ 
  72.    /* '\0' is a char, not the requires char pointer. Try      */
  73.    cpy[0] = '\0'; 
  74.    saved = cpy;
  75.    while (*cmd != '\0')
  76.       {              
  77.       
  78.       /* if (*cmd == '/') modified to avoid duplicate code */
  79.       if ( (*cmd == '/') || (*cmd == '-') )
  80.          {
  81.          if (i == MAXSWITCH)   /* test switch limit */
  82.             {
  83.             printf("INT: Too many switches ...\n");
  84.             return 9999;  /* wouldn't it be easier to return a negative? */
  85.             }
  86.  
  87.          *cpy = *cmd;          /* parse switch to cpy (saved) */
  88.          do
  89.             {
  90.             *cpy++ = *cmd++;
  91.             }
  92.          while (!strchr(" /-\0", *cmd));
  93.          *cpy = '\0';
  94.          cpy = saved;
  95.  
  96.          strcpy(switches[i], cpy); /* copy switch to switches array */
  97.          cpy[0] = '\0';            /* strcpy(cpy, '\0'); */
  98.          i++;
  99.          }
  100.  
  101. /* this is duplicate code. just add the condition to the prior if 
  102.       if (*cmd == '-')
  103.          {
  104.  
  105.          if (i == MAXSWITCH)
  106.             {
  107.             printf("INT: Too many switches ...\n");
  108.             return 9999;
  109.             }
  110.  
  111.          *cpy = *cmd;
  112.          do
  113.             {
  114.             *cpy++ = *cmd++;
  115.             }
  116.          while (!strchr(" /-\0", *cmd));
  117.          *cpy = '\0';
  118.          cpy = saved;
  119.  
  120.          strcpy(switches[i], cpy);
  121.          strcpy(cpy, '\0');
  122.          i++;
  123.          }                                                        
  124. end duplicate code */
  125.  
  126.       /* if (!strchr("/-\0", *cmd)) make this an else. no conditions needed */
  127.       else
  128.          {
  129.          *cmd++;
  130.          }
  131.       }
  132.    free(cpy);
  133.    return i;
  134.    }
  135.  
  136.  
  137. int main(void)
  138.    {
  139.    int i, j;
  140.    /* clrscr(); not ansi */
  141.    i = get_switches(s, myswitches);   
  142.    printf("Switches found: %i\n", i); 
  143.  
  144.    /* for (i = 0; i < MAXSWITCH; i++) why print blank switches? */
  145.    for (j = 0; j < i; j++)
  146.       {
  147.       printf("Switch: %s\n", myswitches[j]);
  148.       }
  149.    return 0;
  150.    }
  151.  
  152.  
  153. -- 
  154. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  155. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  156.  
  157.